//
//  WaveAnimationView.m
//  YLScaner
//
//  Created by Admin on 2025/9/25.
//

#import "WaveAnimationView.h"

@interface WaveAnimationView ()
@property (nonatomic, strong) UIView *maskLayer; // 半透明遮罩
@property (nonatomic, strong) NSMutableArray<CAShapeLayer *> *waveLayers; // 波纹图层
@property (nonatomic, assign) CGRect scanFrame; // 扫描框范围
@end

@implementation WaveAnimationView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor]; // 背景模拟摄像头画面
        [self setupScanFrame]; // 初始化扫描框
        [self setupMask]; // 初始化遮罩
        [self setupWaves]; // 初始化波纹
    }
    return self;
}

#pragma mark - 初始化扫描框（居中，200x200）
- (void)setupScanFrame {
    CGFloat size = 200;
    self.scanFrame = CGRectMake(
        (self.bounds.size.width - size)/2,
        (self.bounds.size.height - size)/2,
        size,
        size
    );
}

#pragma mark - 初始化遮罩（扫描框外半透明）
- (void)setupMask {
    self.maskLayer = [[UIView alloc] initWithFrame:self.bounds];
    self.maskLayer.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7]; // 70%黑
    [self addSubview:self.maskLayer];
    
    // 扫描框挖空（透明区域）
    UIView *hole = [[UIView alloc] initWithFrame:self.scanFrame];
    hole.backgroundColor = [UIColor clearColor];
    // self.maskLayer.ma = hole; // 关键：通过mask实现挖空
}

#pragma mark - 初始化波纹图层（3个同心圆）
- (void)setupWaves {
    self.waveLayers = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        CAShapeLayer *wave = [CAShapeLayer layer];
        wave.frame = self.scanFrame;
        wave.strokeColor = [UIColor greenColor].CGColor; // 绿色波纹（高对比度）
        wave.lineWidth = 2;
        wave.fillColor = [UIColor clearColor].CGColor;
        [self.maskLayer.layer addSublayer:wave]; // 波纹添加到遮罩层上
        [self.waveLayers addObject:wave];
    }
}

#pragma mark - 启动动画（核心逻辑）
- (void)startAnimation {
    [self stopAnimation]; // 先停止已有动画
    
    CGPoint center = CGPointMake(CGRectGetMidX(self.scanFrame), CGRectGetMidY(self.scanFrame));
    CGFloat maxRadius = self.scanFrame.size.width / 2; // 最大半径
    
    for (int i = 0; i < self.waveLayers.count; i++) {
        CAShapeLayer *wave = self.waveLayers[i];
        CGFloat delay = i * 0.6; // 每个波纹延迟0.6秒启动
        
        // 1. 路径动画（从0到最大半径）
        CABasicAnimation *pathAnim = [CABasicAnimation animationWithKeyPath:@"path"];
        pathAnim.fromValue = (__bridge id)[UIBezierPath bezierPathWithArcCenter:center radius:0 startAngle:0 endAngle:2*M_PI clockwise:YES].CGPath;
        pathAnim.toValue = (__bridge id)[UIBezierPath bezierPathWithArcCenter:center radius:maxRadius startAngle:0 endAngle:2*M_PI clockwise:YES].CGPath;
        
        // 2. 透明度动画（从1到0）
        CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
        opacityAnim.fromValue = @1;
        opacityAnim.toValue = @0;
        
        // 3. 组合动画
        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.animations = @[pathAnim, opacityAnim];
        group.duration = 2; // 单个波纹持续2秒
        group.beginTime = CACurrentMediaTime() + delay;
        group.repeatCount = HUGE_VALF; // 无限循环
        [wave addAnimation:group forKey:@"wave"];
    }
}

#pragma mark - 停止动画
- (void)stopAnimation {
    for (CAShapeLayer *wave in self.waveLayers) {
        [wave removeAllAnimations];
    }
}

#pragma mark - 适配屏幕旋转
- (void)layoutSubviews {
    [super layoutSubviews];
    self.maskLayer.frame = self.bounds;
    [self setupScanFrame]; // 重新计算扫描框位置
   //  self.maskLayer.mask.frame = self.scanFrame; // 更新挖空区域
    for (CAShapeLayer *wave in self.waveLayers) {
        wave.frame = self.scanFrame; // 更新波纹位置
    }
}

@end
